package com.homework.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.homework.bean.UserBean;
import com.homework.service.UserService;
import com.homework.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SetUserInfoAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	UserBean userBean = new UserBean();
	UserService userService = new UserService();
	
	public String execute(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> session = ActionContext.getContext().getSession();
		Object userId = session.get("userId");
		if(userId == null){
			//返回找不到用户
			request.setAttribute("message", Constants.MESSAGE_ERROR_NotFoundUser);
			return ERROR;
		}
		userBean.setUserId(userId.toString());
		
		if(userService.setUserInfo(getUserBean())){
			request.setAttribute("message", Constants.MESSAGE_SUCCESS_ChangeUserInfoSuccess);
			return SUCCESS;
		}else{
			request.setAttribute("message", Constants.MESSAGE_ERROR_ChangeUserInfoFail);
			return ERROR;
		}
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
}
